<?php
/**
 * Created by PhpStorm.
 * User: wanghongbing
 * Date: 2018/11/14
 * Time: 下午3:31
 */

namespace app\Admin\controller;
use think\captcha\Captcha;
use think\Controller;
use think\Model;
use think\facade\Request;
use think\view\driver\Think;
use app\Admin\model\Admin;
use think\Session;
use app\Admin\model\HighLevel;

class Login extends Controller
{
    public function login(){

        return $this -> fetch();
    }

    //验证码方法

    public function verify(){
    //配置相关参数

        $config = [

            //验证码字体大小
            'fontSize' =>15,
            //验证码位数
            'length' =>4,
            //关闭验证码杂点
            'useNoise' =>false,
            //验证码宽度
            'imageW' =>108,
            //验证码高度
            'imageH' =>42,

        ];
      $captcha = new Captcha($config);

      return $captcha ->entry();

    }

    public function checkLogin(){

        //接收所有数据
     $postData =  Request::param();

     if (Admin::Login($postData['username'],$postData['password'],$postData['code'])){

         return $this ->success('登录成功',url('Login/index'));

     }else{
         return $this ->error('用户名或密码验证码错误',url('Login/Login'));
     }
     //验证用户名是否存在
       /* $map = array('username' =>$postData['username']);


      $Users = Users::get($map);
      //$Users要么对象，要么空

      if (!is_null($Users)){

          if($Users -> getData('password') !==$postData['password']){

              //用户名密码错误，跳转登录页面

              return $this -> error('密码错误',url('Login'));
          }else{

              //用户密码正确 admin_id 存session
              session('adminid',$Users ->getData('id'));

              return $this ->success('登录成功',url('index'));
          }

          //验证密码


      }
      else{

          return $this ->error('用户名不存在',url('Login'));
      }



        var_dump($postData);*/

        //接收参数

       /*$code = $this ->request ->param('code');
       $captcha = new Captcha();

       if ($captcha -> check($code)){

           echo '验证码正确';
       }else{

           echo '验证码错误';
       }*/


      /*$verify = $this -> checkCode($code);

      if ($verify){

          echo '验证码正确';
      }
      else{

          echo '验证码错误';
      }*/
    }

    /*//验证码方法

    public function checkCode($code){

      $verify = new \Think\Verify();

     return $this -> check($code);
    }*/

    //首页控制器

    public function index(){

        $title = '控制台首页';

        $user = session('user');

        $this ->assign('title',$title);

        $this ->assign('user',$user);

        return $this -> fetch();


    }

    public function logOut(){

     if (Admin::logout()){

         return $this ->success('注销成功',url('Login/login'));
     }else{

         return $this ->error('注销失败',url('Login/index'));
     }
    }
    //高级分类页面
    public function highlevel(){

     $highlevel = new HighLevel;

     $highlevellist = $highlevel -> select();

     $highleveltitle = '高级分类';

     $user = session('user');

     $this ->assign('highlevellist',$highlevellist);

     $this ->assign('highleveltitle',$highleveltitle);

     $this ->assign('user',$user);

     //var_dump($highlevellist);

     return $this ->fetch();
    }

    //高级分类数据

    public function  highlevellist(){

    $highlevel  =  new HighLevel;

    $datalist = $highlevel ->select();

    $arr = array();

    $arr["code"] = 0;

    $arr["msg"] ="";

    $arr["data"] = $datalist;

    $json_data = json_encode($arr);

    echo $json_data;


    }

}